package com.tuanzi.loan.web.bean.family.sms;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.sms.SmsSendTask;
import com.tuanzi.loan.business.service.sms.SmsSendTaskService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.SmsSendTaskSearchRequest;
import com.tuanzi.loan.vo.response.family.SmsSendTaskSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.SmsSendTaskLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SmsSendTaskBean extends JSFListingBean<SmsSendTaskSearchRequest, SmsSendTaskSearchResponse> {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private SmsSendTaskService smsSendTaskService;

	private String taskErrorReason;
	private String smsPlatformResult;

	private SmsSendTask smsSendTask;

	@PostConstruct
	private void init() {
		initLazyDataModel();
	}

	public void onReset() {
		initLazyDataModel();
	}

	private void initLazyDataModel() {
		$ = new SmsSendTaskSearchRequest();
		lazyDataModel = new SmsSendTaskLazyDataModel($);
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(SMS_SEND_STATUS, SMS_PLATFORM_CHANNEL, SMS_USER_TYPE);
	}

	public String onViewCondition(Long id) {
		return redirect("/views/family/user-info/send-sms/sms-condition-view.xhtml?sendTaskId=" + id);
	}

	public void onViewError(Long id) {
		SmsSendTask smsSendTask = smsSendTaskService.findOne(id);
		taskErrorReason = smsSendTask.getErrorReason();
	}

	public void onViewResult(Long id) {
		SmsSendTask smsSendTask = smsSendTaskService.findOne(id);
		smsPlatformResult = smsSendTask.getResult();
	}

	public void onCancel(Long smsSendTaskId) {
		smsSendTaskService.cancelTask(smsSendTaskId);
		addSuccessfullyMessage();
	}

	public void onResend(Long smsSendTaskId) {
		smsSendTaskService.resend(smsSendTaskId);
		addSuccessfullyMessage();
	}

	public void onSelect(Long id) {
		smsSendTask = smsSendTaskService.findOne(id);
	}

	public void onEdit() {
		smsSendTaskService.saveOrUpdate(smsSendTask);
	}

}
